Lec8
正文
讲解用的代码
import random
def run_training_run_txt_log(epochs, lr):
print(f"Training for {epochs} epochs with learning rate {lr}")
offset = random.random() / 5
for epoch in range(2, epochs):
# simulating a training run
acc = 1 - 2 ** -epoch - random.random() / epoch - offset
loss = 2 ** -epoch + random.random() / epoch + offset
print(f"epoch={epoch}, acc={acc}, loss={loss}")
# run a training run with a learning rate of 0.1
run_training_run_txt_log(epochs=10, lr=0.01)
W&B
import wandb
import random
def run_training_run(epochs, lr):
print(f"Training for {epochs} epochs with learning rate {lr}")
wandb.init(
# Set the project where this run will be logged
project="example",
# Track hyperparameters and run metadata
config={
"learning_rate": lr,
"epochs": epochs,
})
offset = random.random() / 5
print(f"lr: {lr}")
for epoch in range(2, epochs):
# simulating a training run
acc = 1 - 2 ** -epoch - random.random() / epoch - offset
loss = 2 ** -epoch + random.random() / epoch + offset
print(f"epoch={epoch}, acc={acc}, loss={loss}")
wandb.log({"acc": acc, "loss": loss})
wandb.finish()
run_training_run(epochs=10, lr=0.01)
wandb.finish()的作用和含义: 之前已经通过 wandb.log()、wandb.init(config=...)、artifact 上传等操作产生了一些数据,这些数据可能还没有全部同步到 WandB 服务器。finish() 会等待/触发这些已产生的数据完成同步,并把这个 run 标记为结束
Multiple experiments
def run_multiple_training_runs(epochs, lrs):
for epoch in epochs:
for lr in lrs:
run_training_run(epoch, lr)
# Try different values for the learning rate
epochs = [100, 120, 140]
lrs = [0.1, 0.01, 0.001, 0.0001]
run_multiple_training_runs(epochs, lrs)
根据原始教程整理....